/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org

http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package softtech.apps.KillCockroachs;

import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

public class KillCockroachs extends Cocos2dxActivity{
	
	public static Activity _activiy;
	
	static AdView adView;
	
	//put here your id
	final String ADMOB_ID="a153b00c0345a6b";
	
	
	// MobPartner
	public static final String Pool_ID = "54844";

	static MobPartnerAdInterstitial interstitial;
	
	final String TAG = "KILL COCKROACH";
	
    protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);	
		_activiy = this;
		
		final AudioManager mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
		//final int originalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
		
		// load ads
		try {
			LayoutParams adParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);

			adView = new AdView(this, AdSize.BANNER,
					ADMOB_ID);			
			AdRequest request = new AdRequest();
			adView.loadAd(request);
			adView.setGravity(Gravity.BOTTOM|Gravity.CENTER);
			// Adding full screen container
			addContentView(adView, adParams);
			
			
		} catch (Exception e) {
			Log.e(TAG, "error: " + e);
		}
		startService();
	}

    public void startService(){
    	
    	interstitial = new MobPartnerAdInterstitial(_activiy, Pool_ID);
		interstitial.setMobPartnerAdListener(mInsterstitialListener);
	}
    
    public Cocos2dxGLSurfaceView onCreateView() {
    	
    	Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
    	// KillCockroachs should create stencil buffer
    	glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
    	
    	return glSurfaceView;
    }

    static {
        System.loadLibrary("cocos2dcpp");
    }     
    
    static void showAdmobJNI(){
		_activiy.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				adView.setVisibility(View.VISIBLE);
			}
		});
	}

	static void hideAdmobJNI(){
		_activiy.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				adView.setVisibility(View.GONE);
			}
		});
    }

	static void setAdmobVisibleJNI(final int number){
		_activiy.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if(number==0){
		    		adView.setVisibility(View.INVISIBLE);
		    	} else {
		    		adView.setVisibility(View.VISIBLE);
		    	}
			}
		});

    }

	static void setVisibleAdmobJNI(final boolean visible){

		_activiy.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if(visible){
		    		adView.setVisibility(View.VISIBLE);
		    	} else {
		    		adView.setVisibility(View.INVISIBLE);
		    	}
			}
		});
    }

    static void setAdmobVisibilityJNI(final String name){
    	_activiy.runOnUiThread(new Runnable() {
			@Override
			public void run() {
		    	if(name.equals("show")||name=="show"){
		    		adView.setVisibility(View.VISIBLE);
		    	} else {
		    		adView.setVisibility(View.INVISIBLE);
		    	}
			}
		});
    }
    
    static void showModparten(){
    	_activiy.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				interstitial.fetchAds();
		    	interstitial.show();
			}
		});
    	
    }
	
    private MobPartnerAdListener mInsterstitialListener = new MobPartnerAdListener() {
		
		@Override
		public void onStartDownloadAds(MobPartnerAdView arg0) {

			Log.e(TAG, "Downloading Interstitial Ads");
		}
		
		@Override
		public void onLoadAdSucceeded(MobPartnerAdView arg0,
				MobPartnerAdCampaign arg1) {

			Log.e(TAG, "Interstitial Load succeeded");
		}
		
		@Override
		public void onLoadAdFailed(MobPartnerAdView arg0, String arg1) {

			Log.e(TAG, "Interstitial load failed, please check if there is any Interstitial in your pool");
		}
		
		@Override
		public void onAdDisappeared(MobPartnerAdView arg0) {

			
		}
		
		@Override
		public void onAdClicked(MobPartnerAdView arg0, MobPartnerAdObject arg1) {

			
		}
		
		@Override
		public void onAdChanged(MobPartnerAdView arg0, MobPartnerAdObject arg1) {

			
		}
	};
	
    
}
